<template>
	<div>
		<div>个人中心</div>
		<div>当前用户：{{$store.state.user.name}} </div>
		<a href="javascript:;" @click="handleToLogOut">退出</a>
	</div>
</template>

<script>
	import axios from 'axios'
	export default {
		name: 'profile',
		methods: {
			handleToLogOut() {
				this.axios.get('/api2/users/logout').then(res => {
					let status = res.data.status
					if (status === 0) {
						//退出将用户民置为空
						this.$store.commit('user/USER_NAME',{ name:''})
						this.$router.push('/mine/login')
					} else {

					}
				})
			}
		},
		//前置守卫 to 去哪里 from 来自于哪里  如果正确就走next()流程
		beforeRouteEnter(to, from, next) {
			axios.get('/api2/users/getUser').then(res => {
				let status = res.data.status;
				if (status === 0) {
					next(vm=>{
						vm.$store.commit('user/USER_NAME',{ name:res.data.data.username })
					})
				} else { //可以指定跳转的路由
					next('/mine/login')
				}
			})
		},
	}
</script>

<style>
</style>
